/*
   Copyright (C) 2007 Yen-Ju Chen <yjchenx gmail>

   This application is free software; you can redistribute it and/or 
   modify it under the terms of the MIT license. See COPYING.

*/

#import "PDObject.h"
#import <HParser/HParser.h>

NSString *kPubMedPMIDProperty = @"kPubMedPMIDProperty";
NSString *kPubMedTitleProperty = @"kPubMedTitleProperty";
NSString *kPubMedYearProperty = @"kPubMedYearProperty";
NSString *kPubMedJournalProperty = @"kPubMedJournalProperty";
NSString *kPubMedAbstractProperty = @"kPubMedAbstractProperty";
NSString *kPubMedPDFFileProperty = @"kPubMedPDFFileProperty";

@implementation PDObject
/* HParser */
#if 0
- (void) parser: (HParser *) parser
         foundStartElement: (NSString *) tag
         attributes: (NSDictionary *) attributes
         path: (NSString *) xpath
{
//        NSLog(@"<%@ (%@)> (%@)", tag, attributes, xpath);
}
#endif
#if 0
- (void) parser: (HParser *) parser
         foundEndElement: (NSString *) tag
         attributes: (NSDictionary *) attributes
         path: (NSString *) xpath
{
//        NSLog(@"</%@ (%@)> (%@)", tag, attributes, xpath);
}
#endif
- (void) parser: (HParser *) parser
         foundText: (NSString *) text
         attributes: (NSDictionary *) attributes
         path: (NSString *) xpath
{
	if ([xpath hasSuffix: @"PMID"])
	{
		[self setValue: text forProperty: kPubMedPMIDProperty];
	}
	else if ([xpath hasSuffix: @"ArticleTitle"])
	{
		[self setValue: text forProperty: kPubMedTitleProperty];
	}
	else if ([xpath hasSuffix: @"PubDate/Year"])
	{
		int year = [text intValue];
		if (year > 0)
		{
			[self setValue: [NSNumber numberWithInt: year]
			   forProperty: kPubMedYearProperty];
		}
	}
	else if ([xpath hasSuffix: @"AbstractText"])
	{
		[self setValue: text forProperty: kPubMedAbstractProperty];
	}
	else if ([xpath hasSuffix: @"MedlineTA"])
	{
		[self setValue: text forProperty: kPubMedJournalProperty];
	}
}

/* NSObject */
- (id) initWithXML: (NSString *) path
{
	self = [self init];

	CREATE_AUTORELEASE_POOL(x);
	HParser *parser = [[HParser alloc] initWithContentsOfFile: path];
	if (parser)
	{
		[parser setUsePath: YES];
		[parser setDelegate: self];
		[parser parse];
		DESTROY(parser);
		DESTROY(x);
	}
	else
	{
		[self dealloc];
		DESTROY(x);
		return nil;
	}
	return self;
}

+ (void) initialize
{
	/* GNU ObjC runtime does not call +initialize in super.
	   So we have to include all properties from OKObject */
    NSDictionary *pt = [OKObject propertiesAndTypes];
    [PDObject addPropertiesAndTypes: pt];
    pt = [[NSDictionary alloc] initWithObjectsAndKeys:
        [NSNumber numberWithInt: kOKStringProperty],
            kPubMedPMIDProperty,
        [NSNumber numberWithInt: kOKStringProperty],
            kPubMedTitleProperty,
        [NSNumber numberWithInt: kOKStringProperty],
            kPubMedPDFFileProperty,
        [NSNumber numberWithInt: kOKIntegerProperty],
            kPubMedYearProperty,
        [NSNumber numberWithInt: kOKStringProperty],
            kPubMedJournalProperty,
        [NSNumber numberWithInt: kOKStringProperty],
            kPubMedAbstractProperty,
        nil];
    [PDObject addPropertiesAndTypes: pt];
    DESTROY(pt);
}

@end

